<template>
  <div class="app-container">
     <el-button
            size="mini"
            type="primary"
            @click="handleAdd(0, 1)"
            >添加</el-button
          >
    <el-table :data="tableData.slice((currentPage3-1)*pagesize,currentPage3*pagesize)" style="width: 100%">
      <el-table-column label="orgID" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.orgID }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.note }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" width="230">
        <template slot-scope="scope">
          <span>{{ scope.row.createAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.protocol }}</span>
        </template>
      </el-table-column>
      <el-table-column label="版本" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.version }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleAdd(scope.$index, 0)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pages" v-if="tableData!=''">
      <div class="block">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage3"
          :page-size="pagesize"
          layout="prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :title="addnum == 1 ? '添加' : '修改'"
      :visible.sync="dialogFormVisibleadd"
      width="30%"
    >
      <el-form
        :model="addform"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
       <el-form-item label="libID" prop="pass">
          <el-input
            type="text"
            v-model="addform.libID"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item v-if="addnum == 0" label="orgID" prop="pass">
          <el-input
            type="text"
            v-model="addform.orgID"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="pass">
          <el-input
            type="text"
            v-model="addform.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="记录" prop="pass">
          <el-input
            type="text"
            v-model="addform.note"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="版本" prop="pass">
          <el-input
            type="text"
            v-model="addform.version"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="协议" prop="pass">
          <el-input
            type="text"
            v-model="addform.protocol"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="handleAdds"
            ><span v-if="addnum == 1">添加</span
            ><span v-else>修改</span></el-button
          >
          <el-button size="mini" @click="Cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import Method from "@/utils/proto";
import { posts } from "@/api/api";
import Error from '@/utils/error'
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisibleadd: false,
      currentPage3: 1,
      pagesize: 15,
      total: 100,
      addnum:'',
      addform: {
        libID: "",
        name: "",
        note: "",
        version: "",
        protocol: "",
      },
    };
  },
  methods: {
    
    Cancel() {
      this.dialogFormVisibleadd = false;
      this.dialogTableVisiblelook = false;
    },
    handleCurrentChange(val) {
    },
    handleAdd(index, num) {
      let roles = this.$store.getters.roles
        if(roles==10000000||roles==11000000||roles=='00000000'){
         this.$message({
           type:'warning',
           message:'没有权限执行此操作'
         })
         return
       }
      this.dialogFormVisibleadd = true;
      this.addnum = num;
      if (num == 0) {
        this.addform.libID = this.tableData[index].libID;
        this.addform.orgID = this.tableData[index].orgID;
        this.addform.name = this.tableData[index].name;
        this.addform.note = this.tableData[index].note;
        this.addform.version = this.tableData[index].version;
        this.addform.protocol = this.tableData[index].protocol;
      }else{
        this.addform.libID = '';
        this.addform.orgID = '';
        this.addform.name = '';
        this.addform.note = '';
        this.addform.version = '';
        this.addform.protocol = '';
      }
    },
    handleAdds() {
      const that = this;
      let data = this.addform;
      let num = that.addnum;

      if (num == 1) {
        data.method = Method.Method.MSG_AGENT_LIB_ADD;
      } else {
        data.method = Method.Method.MSG_AGENT_LIB_UPDATE;
      }
      data.token = this.$store.getters.token;
      posts(data)
        .then((res) => {
          console.log(res,data,'rd');
          if(res.result == 0 ){
           this.onloadlist();
          this.dialogFormVisibleadd = false;
          this.$message({
           type:'success',
           message:'修改成功'
         })
          }else{
            this.$message({
                  type: 'error',
                  message: Error.getErrorMessage(res.result),
                });
         }
          
         
        })
        .catch();
    },
    handleUpload() {},
    onloadlist() {
      let data = {
        method: Method.Method.MSG_AGENT_LIB_LIST,
        token: this.$store.getters.token,
      };
      posts(data)
        .then((res) => {
          console.log(res,'qlist');
          this.tableData = res.list;
          this.dialogFormVisibleu = false;
          this.total = res.list.length;
        })
        .catch();
    },
  },
  created: function () {
    this.onloadlist();
  },
};
</script>

<style lang="scss" scoped>
.pages {
  width: 100%;
  margin-top: 2%;
  display: flex;
  justify-content: center;
}
</style>
